package com.ruicar.afs.cloud.afscase.loanflawfix.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseChannelInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CaseBackReasonInfo;
import com.ruicar.afs.cloud.afscase.loanflawfix.condition.LoanFlawFixCondition;
import com.ruicar.afs.cloud.afscase.loanflawfix.entity.LoanFlawFix;
import com.ruicar.afs.cloud.afscase.loanflawfix.service.LoanFlawFixService;
import com.ruicar.afs.cloud.afscase.loanflawfix.vo.LoanFixFileTypeVo;
import com.ruicar.afs.cloud.afscase.loanflawfix.vo.LoanFlawFixParamsVo;
import com.ruicar.afs.cloud.afscase.loanflawfix.vo.LoanFlawFixVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.enums.BusiNodeEnum;
import com.ruicar.afs.cloud.image.service.ComAttachmentManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rongji.zhang
 * @Date 2020/8/26
 * @description
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/loanFlawFix")
@Api("激活规则配置")
public class LoanFlawFixController {

    private LoanFlawFixService loanFlawFixService;
    private CaseBaseInfoService caseBaseInfoService;
    private CaseChannelInfoService caseChannelInfoService;
    private CaseContractInfoService caseContractInfoService;
    private ComAttachmentManagementService comAttachmentManagementService;
    /**
     * 查询瑕疵申请所有数据
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getLoanFlawFixList")
    @ApiOperation(value = "查询放款审核页面所有数据")
    public IResponse<LoanFlawFix> getLoanFlawFixList(@RequestBody LoanFlawFixCondition condition) {
        IPage<LoanFlawFixVo> loanFlawFixVoList = loanFlawFixService.getLoanFlawFixList(new Page(condition.getPageNumber(),condition.getPageSize()),condition);
//        AfsUser user = SecurityUtils.getUser();
        return IResponse.success(null!=loanFlawFixVoList?loanFlawFixVoList:"");
    }

    /**
     * 根据ID删除瑕疵申请
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/delFlawFix")
    @ApiOperation(value = "根据ID删除瑕疵申请")
    public IResponse<Boolean> delFlawFix(@RequestBody LoanFlawFixCondition condition) {
        loanFlawFixService.removeById(condition.getId());
        return new IResponse<Boolean>().setMsg("删除成功！");
    }

    /**
     * 根据申请编号查询页面参数
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/findParamsByApplyNo")
    @ApiOperation(value = "根据申请编号查询页面参数")
    public IResponse<LoanFlawFixParamsVo> findParamsByApplyNo(@RequestBody LoanFlawFixCondition condition) {
        CaseBaseInfo caseBaseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                .eq(CaseBaseInfo::getApplyNo,condition.getApplyNo()));
        if(null!= caseBaseInfo){
            LoanFlawFixParamsVo loanFlawFixParamsVo = new LoanFlawFixParamsVo();
            BeanUtil.copyProperties(caseBaseInfo, loanFlawFixParamsVo);
            //合作商信息
            CaseChannelInfo caseChannelInfo = caseChannelInfoService.getOne(Wrappers.<CaseChannelInfo>query().lambda()
                    .eq(CaseChannelInfo::getApplyNo,caseBaseInfo.getApplyNo()));
            loanFlawFixParamsVo.setDealerName(caseChannelInfo.getDealerName());
            //案件合同信息
            CaseContractInfo caseContractInfo = caseContractInfoService.getOne(Wrappers.<CaseContractInfo>query().lambda()
                    .eq(CaseContractInfo::getApplyNo,caseBaseInfo.getApplyNo()));
            loanFlawFixParamsVo.setLedingPassDate(caseContractInfo.getLendingPassDate());
            loanFlawFixParamsVo.setApplyStatus(caseContractInfo.getApplyStatus());
            return IResponse.success(loanFlawFixParamsVo);
        } else {
            return IResponse.fail("未找到该申请编号的详细信息。");
        }
    }

    /**
     * 根据申请编号查询页面参数
     * @return
     */
    @PostMapping(value = "/getFixFileTypeList")
    @ApiOperation(value = "获取修正文件类型")
    public IResponse getFixFileTypeList() {
        List<ComAttachmentManagement> loanApplyList = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .eq(ComAttachmentManagement::getBusiNode, BusiNodeEnum.LOAN_APPLY.getCode()));
        List<LoanFixFileTypeVo> resultLoanApplyList =new ArrayList<>();
        List<LoanFixFileTypeVo> resultOrderApplyList =new ArrayList<>();
        List resultList = new ArrayList();
        loanApplyList.forEach(column->{
            LoanFixFileTypeVo caseBackReason =new LoanFixFileTypeVo();
            caseBackReason.setLabel(column.getAttachmentName());
            caseBackReason.setValue(column.getId().toString());
            resultLoanApplyList.add(caseBackReason);
        });
        List<ComAttachmentManagement> orderApplylist = comAttachmentManagementService.list(Wrappers.<ComAttachmentManagement>query().lambda()
                .eq(ComAttachmentManagement::getBusiNode, BusiNodeEnum.ORDER_APPLY.getCode()));
        orderApplylist.forEach(column->{
            LoanFixFileTypeVo caseBackReason =new LoanFixFileTypeVo();
            caseBackReason.setLabel(column.getAttachmentName());
            caseBackReason.setValue(column.getId().toString());
            resultOrderApplyList.add(caseBackReason);
        });
        LoanFixFileTypeVo loanApplyFileType = new LoanFixFileTypeVo();
        LoanFixFileTypeVo orderApplyFileType = new LoanFixFileTypeVo();
        loanApplyFileType.setLabel(BusiNodeEnum.LOAN_APPLY.getDesc());
        loanApplyFileType.setValue(BusiNodeEnum.LOAN_APPLY.getCode());
        loanApplyFileType.setChildren(resultLoanApplyList);
        orderApplyFileType.setLabel(BusiNodeEnum.ORDER_APPLY.getDesc());
        orderApplyFileType.setValue(BusiNodeEnum.ORDER_APPLY.getCode());
        orderApplyFileType.setChildren(resultOrderApplyList);
        resultList.add(loanApplyFileType);
        resultList.add(orderApplyFileType);
        return IResponse.success(resultList);
    }
}
